package com.dhnsoft.education.service.userCenter.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhnsoft.education.entity.userCenter.UcenterMember;
import com.dhnsoft.education.entity.userCenter.vo.PwdVo;
import com.dhnsoft.education.entity.userCenter.vo.RegisterVo;
import com.dhnsoft.education.handle.EduException;
import com.dhnsoft.education.mapper.userCenter.UcenterMemberMapper;
import com.dhnsoft.education.service.userCenter.UcenterMemberService;
import com.dhnsoft.education.utils.JwtUtils;
import com.dhnsoft.education.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author 代浩楠
 * @since 2022-04-05
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    private String[] sysKeyType = {"mobile", "email"};
    //登录的方法
    @Override
    public String login(UcenterMember ucenterMember) {
        //获取登录的邮箱号和密码
        String email = ucenterMember.getEmail();
        String password = ucenterMember.getPassword();

        //手机号和密码非空判断
        if(StringUtils.isEmpty(email)|| StringUtils.isEmpty(password)){
            throw new EduException(20001,"登录失败");
        }
        //判断邮箱是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("email",email);
        UcenterMember mobileMember = this.getOne(wrapper);
        //判断邮箱是否正确
        if(StringUtils.isEmpty(mobileMember)){
            throw new EduException(20001,"登陆失败");
        }
        //判断密码是否正确
        //因为存储到数据库密码肯定是加密的
        //把输入的密码进行加密，再和数据库密码进行比较.
        //加密方式 MD5
        if(!MD5.encrypt(password).equals(mobileMember.getPassword())){
            throw new EduException(20001,"密码错误");
        }
        //判断用户是否禁用
        if(mobileMember.getIsDisabled()==true){
            throw new EduException(20001,"用户被禁用");
        }
        //登录成功
        //生成token字符串，使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());


        return jwtToken;
    }

    @Override
    public boolean register(RegisterVo registerVo) {
        //获取注册信息，进行校验
        String nickname = registerVo.getNickname();//昵称
        //String mobile = registerVo.getMobile();//电话号码
        String email = registerVo.getEmail();//邮箱
        //String regType = registerVo.getRegType();//注册方式
        String password = registerVo.getPassword();//注册密码
        String code = registerVo.getCode();//验证码

        //安全校验
        //if (!Arrays.asList(sysKeyType).contains(regType)) throw new EduException(20001, "非法篡改数据");

        //boolean isMobile = regType.equals("mobile");
        //注册类型的值 手机号 或者邮箱
        //String regTypeValue = isMobile ? mobile : email;
        //校验参数
        //if (isMobile && StringUtils.isEmpty(mobile)) throw new EduException(20001, "请输入手机号");
        if (StringUtils.isEmpty(email)) throw new EduException(20001, "请输入邮箱");
        if (StringUtils.isEmpty(password)) throw new EduException(20001, "请输入密码");
        if (StringUtils.isEmpty(code)) throw new EduException(20001, "请输入验证码");

        //校验校验验证码
        //从redis获取发送的验证码
        String mobleCode = redisTemplate.opsForValue().get(email);
        if (!code.equals(mobleCode)) throw new EduException(20001, "验证码错误");

        //查询数据库中是否存在相同的手机号码 or 邮箱
        Integer count = baseMapper.selectCount(new QueryWrapper<UcenterMember>().eq("email", email));
        if (count.intValue() > 0) throw new EduException(20001, "该邮箱已注册");

        //添加注册信息到数据库
        UcenterMember member = new UcenterMember();
        member.setNickname(nickname);
        //member.setMobile(registerVo.getMobile());
        member.setEmail(registerVo.getEmail());
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        boolean save = this.save(member);
        return save;
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = this.baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public int countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }

    @Override
    public UcenterMember selectOne(long id) {
        UcenterMember member = baseMapper.selectById(id);
        return member;
    }

    @Override
    public boolean updatePwd(PwdVo pwdVo) {
        String code = pwdVo.getCode();//验证码
        String email = pwdVo.getEmail();//邮箱
        String password = pwdVo.getPassword();//密码
        String userId = pwdVo.getUserId();//用户id
        if (StringUtils.isEmpty(email)) throw new EduException(20001, "请输入邮箱");
        if (StringUtils.isEmpty(password)) throw new EduException(20001, "请输入密码");
        if (StringUtils.isEmpty(code)) throw new EduException(20001, "请输入验证码");
        //校验校验验证码
        //从redis获取发送的验证码
        String mobleCode = redisTemplate.opsForValue().get(email);
        if (!code.equals(mobleCode)) throw new EduException(20001, "验证码错误");
        UcenterMember member = baseMapper.selectById(userId);
        member.setPassword(MD5.encrypt(password));
        int i = baseMapper.updateById(member);
        if(i>0){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        String encrypt = MD5.encrypt("123");
        System.out.println(encrypt);
    }
}
